// import {getUserInfo} from '../../utils/apiData'
import {
  wxapp
  } from '../../api/apiData'
  Page({
    /**
     * 页面的初始数据
     */
    data: {
      type: '',
      token: '',
      tokenS: '',
      groupId: '',
      buttonClicked: true,
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this
      wx.login({
        success(res) {
          that.setData({
            code: res.code
          })
        }
      })
    },
  
    /**
     * 授权登录
     */
    getUserInfos(e) {
      this.setData({
        buttonClicked: false
      })
      const that = this;
      const {
        code
      } = that.data
      console.log(code)
        wx.getUserInfo({
          success: (res) => {
            var encryptedData = res.encryptedData;
            var iv = res.iv
            var info = res.rawData
            var obj = {
              code: code,
              iv: iv,
              encryptedData: encryptedData
            }
            wxapp.login(obj, res => {
              if (res.code == 1) {
                wx.setStorage({
                  key: "token",
                  data: res.data.token
                })
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                that.setData({
                  buttonClicked: true
                })
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            })
          },
          fail: res => {
            that.setData({
              buttonClicked: true
            })
          }
        })
    
    },
  
    /**
     * 暂不登录
     */
    onNotLogin() {
      let _this = this;
      // 跳转回原页面
      wx.reLaunch({
        url: '/pages/index/index',
      })
    },
  
    /**
     * 授权成功 跳转回原页面
     */
    onNavigateBack() {
      wx.navigateBack();
    },
  
  })